package com.sise.cn.handle;

import com.sise.cn.vo.HttpBody;
import com.sise.cn.enums.error.SystemError;
import com.sise.cn.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName ExecptionHandle
 * @Description
 * @Author CCJ
 * @Date 2020/6/5 18:57
 **/
@Slf4j
@RestControllerAdvice
public class ExceptionHandle {

    @Autowired
    private Handle handle;

    @ExceptionHandler(value = Exception.class)
    public HttpBody handle(Exception e) {
        log.error("", e);
        HttpBody body = handle.handle(e);
        if (body != null) {
            return body;
        }
        if (e instanceof SystemException) {
            SystemException error = (SystemException) e;
            return HttpBody.getInstance(error.getCode(), error.getMsg());
        }
        return HttpBody.getInstance(SystemError.UNKNOWN_ERROR.getCode(), SystemError.UNKNOWN_ERROR.getMessage());
    }

}
